#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpf_datasource_base.py 
@version:
@time: 2022/03/11 
"""
from TapClientCore.T05_TM_feature.v2.TMF01_datasource.pdk_http import PDKHttp
from TapUtil.logfunc.logger import logger
from TapClientCore.T01_init_conf.v2_init.init_config import MFA, config, datasource_collection
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm
from TapException.TM import DeleteDataSourceError
from TapUtil.decorator.some_useful import timeout, func_logger


class V2PFDataSourceBase:
    TMFHTTP: PDKHttp = PDKHttp()
    serve = config.get('serve')
    user = v2tm.user

    def datasource_test(self, name):
        self.TMFHTTP.test_datasource_by_name(name)
        self._wait_datasource_schema(name)
        self._wait_datasource_ready(name)

    def load_datasource_schema(self, name):
        self.TMFHTTP.ws_loading_schema_by_name(name)
        self._wait_datasource_schema(name)

    @timeout('finished', 200)
    def _wait_datasource_schema(self, name):
        response = self.TMFHTTP.pdk_query_by_name(name)
        return response.get('loadFieldsStatus')

    @timeout('ready', 200)
    def _wait_datasource_ready(self, name):
        response = self.TMFHTTP.pdk_query_by_name(name)
        return response.get('status')

    @func_logger
    def datasource_insert_mongodb(self, db_info, connection_id):
        name = db_info['name']
        MFA.insert_one(datasource_collection,
                       {'name': name,
                        'serve': self.serve,
                        'info': db_info,
                        'id': connection_id,
                        'user': self.user})

    @func_logger
    def datasource_delete_mongodb(self, db_info):
        name = db_info['name']
        MFA.delete_one(datasource_collection,
                       {'name': name, 'serve': self.serve, 'user': self.user})

    @func_logger
    def datasource_query_mongodb(self, db_info):
        name = db_info['name']
        query_result = MFA.query(datasource_collection,
                                 {'name': name,
                                  'serve': self.serve,
                                  'user': self.user})
        if query_result:
            return query_result[0]
        return query_result

    @func_logger
    def datasource_update_mongodb(self, db_info, connection_id, update_info=None):
        name = db_info['name']
        if update_info:
            return MFA.update(datasource_collection,
                              {'name': name, 'serve': self.serve, 'user': self.user},
                              {'$set': {'id': connection_id, 'info': update_info, 'name': update_info['name']}})
        return MFA.update(datasource_collection,
                          {'name': name, 'serve': self.serve, 'user': self.user},
                          {'$set': {'id': connection_id, 'info': db_info}})

    @func_logger
    def delete_record_data_source_by_filter(self, filter=None):
        if filter is None:
            filter = {}
        result = MFA.query(datasource_collection, {
            'serve': self.serve,
            **filter
        })
        for i in result:
            try:
                result = self.TMFHTTP.pdk_delete_datasource(i['name'])
            except DeleteDataSourceError as e:
                logger.warning(f"DataSource has been used")
            MFA.delete_one(datasource_collection,
                           {'serve': self.serve, "name": i["name"], 'user': self.user})
